package com.adams.custom.page.core.dao.impl;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.apache.log4j.Logger;

import com.adams.access.db.bean.DicTable;
import com.adams.access.db.dao.hibernate.HibernateGenericDao;
import com.adams.core.exception.BusinessException;
import com.adams.custom.page.util.ViewCacheUtil;

/**
 * 
 * File Name : DicTableDaoImpl.java
 * 
 * @Description : <Detail Description for Class >
 */
public class DicTableDaoImpl extends HibernateGenericDao<DicTable, String>
{
	Logger logger = Logger.getLogger(DicTableDaoImpl.class.getName());

	private static String cacheMap_key = "DicTable_cacheMap";

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.adams.access.db.dao.hibernate.HibernateGenericDao#save(java
	 * .lang.Object)
	 */
	// public void save(DicTable model)
	// {
	// super.save(model);
	// }
	/**
	 * Description : 根据tablecode获得表定义
	 * 
	 * @param tablecode
	 * @return
	 * @throws Exception
	 * 
	 */
	@SuppressWarnings("unchecked")
	public DicTable getDicTable(String tablecode) throws BusinessException
	{
		Map cacheMap = ViewCacheUtil.getConfigDataCacheMap(cacheMap_key);
		if (!cacheMap.containsKey(tablecode))
		{
			synchronizationCache();
		}
		DicTable result = (DicTable) cacheMap.get(tablecode);
		if (null == result)
		{
			logger.error(tablecode + "表定义不存在！");
			throw new BusinessException("table.config.notexists.exception");
		}
		return result;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.adams.access.db.dao.hibernate.HibernateGenericDao#delete(java
	 * .lang.Object)
	 */
	// public void delete(DicTable model)
	// {
	// super.delete(model);
	// }
	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.adams.access.db.dao.hibernate.HibernateGenericDao#saveOrUpdate
	 * (java.lang.Object)
	 */
	// public void saveOrUpdate(DicTable model)
	// {
	// super.saveOrUpdate(model);
	// }
	/**
	 * Description : 得到所有的表定义对象
	 * 
	 * @return
	 * 
	 */
	@SuppressWarnings("unchecked")
	public List<DicTable> findAll()
	{
		List<DicTable> result = null;
		Map cacheMap = ViewCacheUtil.getConfigDataCacheMap(cacheMap_key);
		if (cacheMap.size() == 0)
		{
			result = synchronizationCache();
			return result;
		} else
		{
			result = new ArrayList<DicTable>();
			Set keySet = cacheMap.keySet();
			for (Iterator it = keySet.iterator(); it.hasNext();)
			{
				Object key = it.next();
				DicTable temp = (DicTable) cacheMap.get(key);
				if (null != temp)
				{
					result.add(temp);
				}
			}
		}
		return result;
	}

	private List<DicTable> findAllFromDB()
	{
		List<DicTable> result = query("from DicTable");
		return result;
	}

	/**
	 * Description : 同步缓存
	 * 
	 * @return
	 * 
	 */
	@SuppressWarnings("unchecked")
	public synchronized List<DicTable> synchronizationCache()
	{
		List<DicTable> result = findAllFromDB();
		Map cacheMap = ViewCacheUtil.getConfigDataCacheMap(cacheMap_key);
		for (DicTable dicTable : result)
		{
			cacheMap.put(dicTable.getTablecode(), dicTable);
		}
		ViewCacheUtil.put2ConfigDataCacheMap(cacheMap_key, cacheMap);
		return result;
	}
}
